#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Entity;
using Library.Services;

#endregion

namespace Library
{
    public partial class FormMember : Form
    {
        Member m_Member;
        User m_currnUser;
        MemberService m_MemberService = new MemberService();
        MemberTypeService m_MemberTypeService = new MemberTypeService();
        MemberClassService m_MemberClassService = new MemberClassService();
        UserControlMember m_UserControlMember;
        bool m_apply = false;

        public FormMember(Member Member, User currnMember, UserControlMember MemberControlMember)
        {
            InitializeComponent();
            labelTitle.Text = "Member";
            Text = "Member";
            m_Member = Member;
            m_currnUser = currnMember;
            m_UserControlMember = MemberControlMember;
            initComboData();
            loadMember();
            initEnterKeyDown(this.Controls);
        }

        private void initComboData()
        {
            comboBoxMemberType.DataSource = m_MemberTypeService.GetAllMemberType();
            comboBoxMemberClass.DataSource = m_MemberClassService.GetAllMemberClass();
            comboBoxGender.DataSource = Enum.GetValues(typeof(Gender));
            comboBoxIdCardType.DataSource = Enum.GetValues(typeof(IDCardType));
        }

        private void initEnterKeyDown(Control.ControlCollection controls)
        {
            foreach (Control c in controls)
            {
                if (c is TextBox)
                {
                    TextBox txtB = (TextBox)c;
                    txtB.EnterKeyDown += new KeyEventHandler(txtB_EnterKeyDown);
                }
                if (c is CheckBox)
                {
                    CheckBox txtB = (CheckBox)c;
                    txtB.KeyDown += new KeyEventHandler(txtB_KeyDown);
                }
                if (c is ComboBox)
                {
                    ComboBox txtB = (ComboBox)c;
                    txtB.KeyDown += new KeyEventHandler(txtB_KeyDown);
                }
                if (c is NumericUpDown)
                {
                    NumericUpDown txtB = (NumericUpDown)c;
                    txtB.KeyDown += new KeyEventHandler(txtB_KeyDown);
                }
                if (c is DateTimePicker)
                {
                    DateTimePicker txtB = (DateTimePicker)c;
                    txtB.KeyDown += new KeyEventHandler(txtB_KeyDown);
                }
                initEnterKeyDown(c.Controls);
            }
        }

        void txtB_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            if (objArgs.KeyCode == Keys.Enter)
            {
                Control ctrl = (Control)objSender;
                SelectNextControl(ctrl, true, true, true, true);
            }
        }

        void txtB_EnterKeyDown(object objSender, KeyEventArgs objArgs)
        {
            Control ctrl = (Control)objSender;
            SelectNextControl(ctrl, true, true, true, true);
        }

        private void loadMember()
        {
            textBoxCode.Text = m_Member.Code;
            textBoxName.Text = m_Member.Name;
            textBoxRemark.Text = m_Member.Remark;

            dateTimePickerDateJoin.Value = m_Member.JoinDate;
            comboBoxGender.SelectedItem = m_Member.Gender;
            textBoxAddress.Text = m_Member.Address;
            textBoxPhone.Text = m_Member.Phone;
            textBoxIDCardNo.Text = m_Member.IDCardNo;
            comboBoxIdCardType.SelectedItem = m_Member.IDCardType;
            numericUpDownMaxBorrow.Value = m_Member.MaxBorrow;
            if (m_Member.Id > 0)
            {
                comboBoxMemberType.Text = m_MemberTypeService.GetMemberTypeById(m_Member.MemberTypeId).ToString();
                comboBoxMemberClass.Text = m_MemberClassService.GetMemberClassById(m_Member.MemberClassId).ToString();
            }
            else
                buttonAutoCode_Click(null, null);
            textBoxCode.Enabled = m_Member.Id == 0;
            buttonApply.Visible = m_Member.Id != 0;
            buttonAutoCode.Visible = m_Member.Id == 0;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (isValid())
            {
                UpdateEntity();
                if (m_Member.Id == 0)
                {
                    m_MemberService.InsertNewMember(m_Member);
                    m_UserControlMember.AddNewData(m_Member);
                }
                else
                {
                    m_MemberService.UpdateMember(m_Member);
                    m_UserControlMember.UpdateData(m_Member);
                }
                MessageBox.Show("Record has been saved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, new EventHandler(CloseForm));
               
            }
        }
        void CloseForm(object sender, EventArgs e)
        {
            if (!m_apply)
                Close();
            m_apply = false;
        }

        private void UpdateEntity()
        {
            m_Member.Code = textBoxCode.Text;
            m_Member.Name = textBoxName.Text;
            m_Member.Remark = textBoxRemark.Text;
            m_Member.CreatedUserId = m_currnUser.Id;
            m_Member.ModifiedUserId = m_currnUser.Id;

            m_Member.MemberTypeId = ((MemberType)comboBoxMemberType.SelectedItem).Id;
            m_Member.JoinDate = dateTimePickerDateJoin.Value;
            m_Member.Gender = (Gender)comboBoxGender.SelectedItem;
            m_Member.Address = textBoxAddress.Text;
            m_Member.Phone = textBoxPhone.Text;
            m_Member.IDCardNo = textBoxIDCardNo.Text;
            m_Member.IDCardType = (IDCardType)comboBoxIdCardType.SelectedItem;
            m_Member.MaxBorrow = Convert.ToInt32(numericUpDownMaxBorrow.Value);
            m_Member.MemberClassId = ((MemberClass)comboBoxMemberClass.SelectedItem).Id;
            
        }

        private bool isValid()
        {
            clearError();
            bool validcode = textBoxCode.Text != "";
            bool validname = textBoxName.Text != "";
            bool validexist = true;
            if (m_Member.Id == 0)
                validexist = m_MemberService.GetMemberByMemberCode(textBoxCode.Text.Trim()) == null;
            if (!validcode) errorProvider.SetError(textBoxCode, "Fill Code");
            if (!validname) errorProvider.SetError(textBoxName, "Fill Name");
            if (!validexist) errorProvider.SetError(textBoxCode, "Code already exist");
            return validcode && validname && validexist;
        }

        private void clearError()
        {
            errorProvider.SetError(textBoxCode, "");
            errorProvider.SetError(textBoxName, "");
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            m_apply = true;
            buttonSave_Click(sender, e);
        }

        private void buttonAutoCode_Click(object sender, EventArgs e)
        {
            textBoxCode.Text = m_MemberService.GenerateCode();
        }

    }
}